$(() => {
    $("#addToCartBtn").click(function() {
        let number = 1; // 商品数量，固定为1
        let id = $("#addToCartBtn").data("id"); // 从按钮的data属性中获取商品ID
        console.log(id,number);
        $.ajax({
            url:ctx+"/shopping/cart",
            method:"post",
            data:{
                id:id,
                number:number,
            },
            success: function (resp) {
                if (resp.code === "200") {
                    Swal.fire({
                        title: "确认框",
                        text: "添加成功",
                        icon: "success",iconColor:"#1d953f",
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消'
                    }).then((flag) => {
                            if (flag.value) {
                                window.location.reload();
                            }
                        }
                    );
                } else {
                    Swal.fire({
                        text: resp.msg,
                        icon: "error",iconColor:"#f05b72",
                    });
                }
            },
            error: function () {
                Swal.fire({
                    text: "操作失败",
                    icon: "error",iconColor:"#f05b72",
                });
            }
        })
    });

    $("#buyNowBtn").click(function() {
        let number = 1; // 商品数量，固定为1
        let id = $(this).data("id"); // 注意这里用 $(this)，防止拿错元素
        console.log(id);
        $.ajax({
            url: ctx + "/shopping/cart",
            method: "post",
            data: {
                id: id,
                number: number
            },
            success: function(resp) {
                if (resp.code === 200) {
                    window.location.href = ctx + "/shopping/cart/settle";
                } else {
                    Swal.fire({
                        text: resp.msg,
                        icon: "error",
                        iconColor: "#f05b72"
                    });
                }
            },
            error: function() {
                Swal.fire({
                    text: "操作失败",
                    icon: "error",
                    iconColor: "#f05b72"
                });
            }
        });
    });


});
